package com.wpf.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.Field;
import com.wpf.system.entity.FieldBook;
import com.wpf.system.entity.dto.BookRentBuySearchDTO;
import com.wpf.system.entity.vo.FieldBookVO;
import com.wpf.system.mapper.FieldMapper;
import com.wpf.system.service.FieldBookService;
import com.wpf.system.mapper.FieldBookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author ferna
* @description 针对表【field_book(场地预约情况表)】的数据库操作Service实现
* @createDate 2023-01-30 11:10:10
*/
@Service
public class FieldBookServiceImpl extends ServiceImpl<FieldBookMapper, FieldBook> implements FieldBookService{

    @Resource
    private FieldMapper fieldMapper;

    @Override
    public List<FieldBook> getFieldBookByDate(List<Long> fids,String bookDate) {
        return this.baseMapper.getFieldBookByDate(fids,bookDate);
    }

    @Override
    public Boolean checkFieldCanBook(Long fid, Long tid, String bookDate) {
        return Convert.toBool(this.baseMapper.checkFieldCanBook(fid,tid,bookDate));
    }

    @Override
    public List<FieldBook> getFieldBookById(List<Long> ids) {
        return this.baseMapper.selectBatchIds(ids);
    }

    @Override
    public int deleteFieldByIds(List<Long> ids) {
        return this.baseMapper.deleteFieldByIds(ids);
    }

    @Override
    public List<FieldBookVO> getAllFieldBook(BookRentBuySearchDTO bookRentBuySearchDTO) {
        return this.baseMapper.getAllFieldBook(
                bookRentBuySearchDTO.getNo(),
                bookRentBuySearchDTO.getUid(),
                bookRentBuySearchDTO.getName(),
                bookRentBuySearchDTO.getNid(),
                bookRentBuySearchDTO.getUsername(),
                bookRentBuySearchDTO.getPhone(),
                bookRentBuySearchDTO.getStartTime(),
                bookRentBuySearchDTO.getEndTime(),
                bookRentBuySearchDTO.getPage(),
                bookRentBuySearchDTO.getPageSize()
        );
    }

    @Override
    public HashMap<String, Object> getFieldBookChart(Long id, String date) {
        List<Field> fieldList = fieldMapper.selectList(new LambdaQueryWrapper<Field>().eq(Field::getPid,id));
        List<Long> fieldID = new ArrayList<>();
        fieldID.add(id);
        if(fieldList.size()>0){
            for(Field item : fieldList){
                fieldID.add(item.getId());
            }
        }
        //当天预约人数
        List<Long> expectedData = new ArrayList<>();
        //当天入场人数
        List<Long> actualData = new ArrayList<>();
        for(int d = -6;d<=0;d++) {
            Date selectDate = DateUtil.offsetDay(DateUtil.parse(date), d);
            LambdaQueryWrapper<FieldBook> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.in(FieldBook::getFid, fieldID);
            queryWrapper1.in(FieldBook::getStatus, Convert.toList(Long.class, new int[]{1, 2, 6}));
            queryWrapper1.between(FieldBook::getBookStartDate, DateUtil.beginOfDay(selectDate), DateUtil.endOfDay(selectDate));
            expectedData.add(this.baseMapper.selectCount(queryWrapper1));

            LambdaQueryWrapper<FieldBook> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.in(FieldBook::getFid, fieldID);
            queryWrapper2.eq(FieldBook::getStatus, 2);
            queryWrapper2.between(FieldBook::getBookStartDate, DateUtil.beginOfDay(selectDate), DateUtil.endOfDay(selectDate));
            actualData.add(this.baseMapper.selectCount(queryWrapper2));
        }
        HashMap<String,Object> chartData = new HashMap<>();
        chartData.put("expectedData",expectedData);
        chartData.put("actualData",actualData);
        return chartData;
    }

}




